# Pablo Rincon Crespo (C) 2009
# This code is published under the GPL 2 License Terms

import sys
from NMAction import *
from threading import Thread
import time, signal, os
from NMUtils import *
try:
        from scapy.all import *
except:
        try:
                from scapy import *
        except:
                print "Error. Scapy Not Found. apt-get install scapy python-scapy"

from pcap import *


def handler(signum, frame):
	print 'SIGINT CAPTURED. Exiting...', signum
	time.sleep(2)
	exit(30)


class NMMirror(Thread):
	def __init__(self, iface="ath0", filter="", NMActions=None, section=""):
		logger.info("Setting section " + section + " with filter " + filter + " for interface " + iface)
		self.section=section
		self.filter=filter
		self.iface=iface
		self.NMActions=NMActions
		Thread.__init__(self)
		signal.signal(signal.SIGINT, handler)
		conf.verb=0

	def run(self):
		while 1:
			logger.info("Sniffing " + self.iface + " for packets matching '"+self.filter+"'")
			p=sniff(filter=self.filter,prn=self.actions, iface=self.iface)
	
	def actions(self,p):
		conf.verb=0
		for act in self.NMActions:
			if p!=None:
				act.perform(p)
			else:
				logger.error("No packet...? filer " + self.filter +" iface " + self.iface)

#Net Mirror (c) 2009 Pablo Rincon Crespo
